package org.budo.taobao.util;

import com.taobao.api.ApiException;
import com.taobao.api.TaobaoClient;
import com.taobao.api.TaobaoRequest;
import com.taobao.api.TaobaoResponse;
import com.taobao.api.internal.util.StringUtils;
import com.taobao.api.request.TbkTpwdCreateRequest;
import com.taobao.api.response.TbkTpwdCreateResponse;
import com.taobao.api.response.TbkTpwdCreateResponse.MapData;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Slf4j
public class TaobaoClientUtil {
    public static <T extends TaobaoResponse> T execute(TaobaoClient taobaoClient, TaobaoRequest<T> request) {
        try {
            return taobaoClient.execute(request);
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }

    public static String tbkTpwdCreateRequest(TaobaoClient taobaoClient, String text, String url) {
        TbkTpwdCreateRequest req = new TbkTpwdCreateRequest();
        req.setText(text);
        req.setUrl(url);
        TbkTpwdCreateResponse rsp = execute(taobaoClient, req);
        MapData data = rsp.getData();
        String model = null == data ? null : data.getModel();

        if (StringUtils.isEmpty(model)) {
            log.error("#35 rsp=" + rsp.getBody() + ", text=" + text + ", url=" + url);
        }

        return model;
    }
}